package etr.bk.gui;

import etr.bk.gui.rpan.*;
import etr.bk.gui.rpan.image.ImageFotoPan;
import etr.bk.gui.rpan.image.PreviewImagePanel;
import etr.bk.gui.rpan.info.InfoPanel;
import etr.bk.gui.lpan.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;

import org.h2.jdbc.JdbcSQLException;

import etr.bk.db.ConnectDB;
import etr.bk.db.WorkDBImage;
import etr.bk.db.WorkDBInfo;
import etr.bk.db.WrapperFotoPan;
import etr.bk.db.WrapperLPan;
import etr.bk.db.WrapperRPanInfo;

public class MainListener extends MouseAdapter implements ActionListener,
		MouseListener, ComponentListener, DocumentListener, WindowListener {
	private static MainListener listener = null;

	private JFrame parent;

	private JButton buttonAdd;
	private JButton buttonEd;
	private JButton buttonSv;
	private JButton buttonDel;
	private JButton buttonCancel;

	private InfoPanel infPanel;
	private LPanel leftPanel;
	private PreviewImagePanel previewFotoPanel;
	
	private JTabbedPane tabledPan;
	
	private JProgressBar progressBar;
	private JButton imageADD;
	private JButton imageDEL;

	private Connection conn;
	private Connection connThr;
	private WorkDBInfo objWorkDBInfo;
	private WorkDBImage objWorkDBImage;
	
	private JTextField textFieldName;
	private JTextField textFieldSurname;

	private JFileChooser fileChooser;
	private FilterForChooser filter;
	private ImagePreviewer prev;

	private JOptionPane dialogFrame;

	private WrapperRPanInfo lastWrapperForRPan;
	private boolean addEnabled;

	private Integer lastID = null;
	
	private int fotoID;

	private boolean add_update; // add == true update == false
	//-----------------------------------------------------------
	private ImageViewFrame imageViewFrame;
	
	private JButton nextViewButton;
	private JButton previousViewButton;
	
	private int fotoIndex;
	
	//private DefaultListModel<ImageFotoPan> imagesPanDefListMod;
	private Object [] arrayOfImage;
	
	//----------------Menu-----------------------------------
	
	private JMenuItem menuItemPropSet;
	
	private JMenuItem menuItemPropReset;
	
	private JMenuItem menuItemUnload;
	
	private JMenuItem menuItemHelp;
	
	private JMenuItem menuItemExit;
	
	//---------UNLOADER-----------------
	private UnloadDialog unloadDialog = null;
	
	private JButton buttonUnload;
	private JButton buttonCancelExport;
	
	//================================================================
	
	public static MainListener getListener() {
		if (MainListener.listener == null) {
			MainListener.listener = new MainListener();
		}

		return MainListener.listener;
	}

	private MainListener() {
		System.out.println("Start listener");

		// -----------DB CONNECTIVITY ----------------------

		ConnectDB dbConnect = new ConnectDB("proper.txt");
		this.conn = dbConnect.getConnection();
		this.connThr = dbConnect.getConnection();
		
		this.objWorkDBInfo = WorkDBInfo.getDBSinglTone(conn);
		this.objWorkDBImage = new WorkDBImage(connThr);

		// ----FILE CHOOSER--------------------------------

		fileChooser = new JFileChooser();
		filter = new FilterForChooser();
		prev = new ImagePreviewer(fileChooser);
		fileChooser.setCurrentDirectory(new File("."));
		fileChooser.setFileFilter(filter);
		fileChooser.setAccessory(prev);

		// ------------------------------------
		
	}

	// -----------------------------------------------------------------------

	public void actionPerformed(ActionEvent event) {

		if (event.getSource() == this.buttonAdd 
				|| event.getSource() == this.buttonCancel
				||event.getSource() == this.buttonDel 
				|| event.getSource() == this.buttonEd
				||event.getSource() == this.buttonSv){
			
			this.toolBarEvents(event);
		}
		else if (event.getSource() == this.imageADD 
				|| event.getSource() == this.imageDEL){
			
			this.imageAddDellEvants(event);
		}
		else if (event.getSource() == this.menuItemExit 
				|| event.getSource() == this.menuItemHelp 
				|| event.getSource() == this.menuItemPropSet 
				|| event.getSource() == this.menuItemPropReset
				|| event.getSource() == this.menuItemUnload){
			
			this.menuEvents(event);
		}
		else if (event.getSource() == this.buttonUnload 
				|| event.getSource() == this.buttonCancelExport){
			
			this.unloadImageEvents(event);
		}
		else if (event.getSource() == this.nextViewButton
				|| event.getSource() == this.previousViewButton){
			
			this.imageViewEvents(event);
		}
		
	}
	
	//--------------------------------------------------------------------------------
	
	public void mouseClicked(MouseEvent e) {
		boolean flag = this.infPanel.getFlagForFotka();

		if (e.getClickCount() == 2 && flag && e.getSource() != previewFotoPanel) {

			System.out.println("Adddd into DB================= "
					+ e.getClickCount());

			File imageFile;
			
			this.fileChooser.setMultiSelectionEnabled(false);
			this.fileChooser.showOpenDialog(this.parent);
			imageFile = this.fileChooser.getSelectedFile();
			//System.out.println("Fill  "+imageFile.getName());
			BufferedImage tmpImage = this.infPanel.setFotoFile(imageFile);
			infPanel.repaint();
			
			if (this.add_update)this.leftPanel.setTmpPanImg(tmpImage);
			else this.leftPanel.setExistsPanImg(tmpImage);
		}
		
		if (e.getClickCount() == 2 && e.getSource() == previewFotoPanel){
			// image in new frame
			System.out.println("Chain of responsibility "+ fotoID);
			
			this.imageViewFrame.setImage(this.objWorkDBImage.getImage(this.fotoID));
			
			
			
			this.imageViewFrame.setVisible(true);
			
		}
	}
	
	//--------------DocumentListeners metods---------------------------
	
	public void insertUpdate(DocumentEvent arg0) {
		String strTmp = this.textFieldName.getText();
		strTmp = strTmp + "  " + this.textFieldSurname.getText();
		if (strTmp.length() >= 18) strTmp = strTmp.substring(0, 17);
		
		if (add_update)leftPanel.setTmpPanText(strTmp);
		else leftPanel.setExistsPanText(strTmp);
	}

	public void removeUpdate(DocumentEvent arg0) {
		String strTmp = this.textFieldName.getText();
		strTmp = strTmp +"  "+this.textFieldSurname.getText();
		if (strTmp.length() >= 18) strTmp = strTmp.substring(0, 17);
		
		if (add_update)leftPanel.setTmpPanText(strTmp);
		else leftPanel.setExistsPanText(strTmp);
	}
	
	//---actions performed BLOKS----------------------------------------------
	
	private void toolBarEvents(ActionEvent event){
		//-----------ADD ITEM---------------------------------
		if (event.getSource() == this.buttonAdd) {
			System.out.println("Click on Add button "
					+ event.getActionCommand());
			this.tabledPan.setSelectedIndex(0);
			
			this.add_update = true;

			this.imageADD.setEnabled(false);
			this.imageDEL.setEnabled(false);
			
			this.previewFotoPanel.clearItemList();
			
			this.buttonAdd.setVisible(false);
			this.buttonEd.setVisible(false);
			this.buttonDel.setVisible(false);
			this.buttonSv.setVisible(true);
			this.buttonCancel.setVisible(true);

			this.menuItemPropSet.setEnabled(false);
			this.menuItemPropReset.setEnabled(false);
			
			leftPanel.setSelectedModeForPictPan(false);
			
			//------------------------------------------------------
			leftPanel.addNewEmptyItem();
			//------------------------------------------------------

			this.infPanel.clearAllFields();
			this.infPanel.setAllFieldsActive();

		} 
		//---------------SAVE ITEM INTO DB---------------------
		
		else if (event.getSource() == this.buttonSv) {

			ArrayList<String> arrInf = this.infPanel.getInfo();

			if (arrInf != null) {
			
				String notes = this.infPanel.getNotes();
				BufferedImage bufImage = this.infPanel.getImage();
				
				if (this.add_update)
					objWorkDBInfo.insertAllInfoIntoDB(arrInf, bufImage, notes);
				else
					objWorkDBInfo.updateAllInfoIntoDB(lastID, arrInf, bufImage,
							notes);

				leftPanel.setSelectedModeForPictPan(true);

				leftPanel.setItemList(this.getInfoForLPan());
				
				if (add_update) leftPanel.selectOnEnd(); // set select on new LAST item

				infPanel.setAllFieldsInactive();
				
				// add to LPanel
				if (unloadDialog != null) unloadDialog.filListItem();

				this.buttonAdd.setVisible(true);
				this.buttonEd.setVisible(true);
				this.buttonDel.setVisible(true);
				this.buttonSv.setVisible(false);
				this.buttonCancel.setVisible(false);
				
				this.menuItemPropSet.setEnabled(true);
				this.menuItemPropReset.setEnabled(true);
				
				this.imageADD.setEnabled(true);
				this.imageDEL.setEnabled(true);
				
			} 
			else {
				 JOptionPane.showMessageDialog(parent, "Fill mandatory fields (name, surname, sex)!!!", "Fill",JOptionPane.OK_OPTION);
			}
		} 
		//--------DELETE ITEM----------------------------------------
		
		else if (event.getSource() == this.buttonDel) {
			
			this.tabledPan.setSelectedIndex(0);
			
			int selectionForDel = JOptionPane.showConfirmDialog(parent,
					"Are you sure?", "Confirm", JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if (selectionForDel == 0) {

				objWorkDBInfo.deleteRecord(lastID);
				leftPanel.setItemList(this.getInfoForLPan());
				
				infPanel.setAllFieldsInactive();
				
				this.getImagesForFotoPanel(this.lastID);
				
				if (unloadDialog != null) unloadDialog.filListItem();
				
				System.out.println(selectionForDel+"873465782346785");
			}
		} 
		//-----------------CANCEL------------------------------------
		
		else if (event.getSource() == this.buttonCancel) {
			System.out.println("/////id "+this.lastID);
			this.buttonAdd.setVisible(true);
			this.buttonEd.setVisible(true);
			this.buttonDel.setVisible(true);
			this.buttonSv.setVisible(false);
			this.buttonCancel.setVisible(false);
			
			this.menuItemPropSet.setEnabled(true);
			this.menuItemPropReset.setEnabled(true);
			
			this.imageADD.setEnabled(true);
			this.imageDEL.setEnabled(true);
			
			leftPanel.setSelectedModeForPictPan(true);
			System.out.println("/////id 1 "+this.lastID);
			leftPanel.setItemList(this.getInfoForLPan());//---------------
			System.out.println("/////id 2 "+this.lastID);
			this.infPanel.clearAllFields();
			//this.infPanel.setAllInfo(this.lastWrapperForRPan);
			this.infPanel.setAllInfo(objWorkDBInfo.getInfoForRpanInfo(this.lastID));
			this.infPanel.setAllFieldsInactive();
			System.out.println("/////id 3"+this.lastID);
			this.getImagesForFotoPanel(this.lastID);
			
			System.out.println("/////id "+this.lastID);

		} 
		//-----------------EDIT ITEM (UPDATE)----------------------
		else if (event.getSource() == this.buttonEd) {
			
			this.tabledPan.setSelectedIndex(0);
			
			this.add_update = false;
			leftPanel.setSelectedModeForPictPan(false);

			this.imageADD.setEnabled(false);
			this.imageDEL.setEnabled(false);
			
			this.buttonAdd.setVisible(false);
			this.buttonEd.setVisible(false);
			this.buttonDel.setVisible(false);
			this.buttonSv.setVisible(true);
			this.buttonCancel.setVisible(true);
			
			this.menuItemPropSet.setEnabled(false);
			this.menuItemPropReset.setEnabled(false);

			this.infPanel.setAllFieldsActive();
			
			leftPanel.editExistsItem();
		} 
	}
	
	private void imageAddDellEvants(ActionEvent event){
		
		//--------------IMAGE ADD DEL-------------------------------
		if (event.getSource() == this.imageADD) {
			System.out.println("Button image ADD");
			this.imageADD.setEnabled(false);
			this.imageDEL.setEnabled(false);
			
			this.buttonAdd.setEnabled(false);
			this.buttonEd.setEnabled(false);
			this.buttonDel.setEnabled(false);
			
			this.leftPanel.setSelectedModeForPictPan(false);
			
			this.fileChooser.setMultiSelectionEnabled(true);
			this.fileChooser.showOpenDialog(this.parent);
			File [] imagesFiles = fileChooser.getSelectedFiles();
			
			if (imagesFiles.length != 0) {
				System.out.println("Call objWorkDB.setImages");
				objWorkDBImage.setImages(this.lastID, imagesFiles, progressBar);
			}
			else {
				this.imageADD.setEnabled(true);
				this.imageDEL.setEnabled(true);
				
				this.buttonAdd.setEnabled(true);
				this.buttonEd.setEnabled(true);
				this.buttonDel.setEnabled(true);
				
				this.leftPanel.setSelectedModeForPictPan(true);
			}
			
			
			//objWorkDB.getImages(fotoID,progressBar);
			
		} 
		//--------------Delete Image-------------------------
		else if (event.getSource() == this.imageDEL) {

				ArrayList<ImageFotoPan> fotosArr = this.previewFotoPanel.getSelectedFotos();
				if (fotosArr != null){
					
					int selectionForDel = JOptionPane.showConfirmDialog(parent,
							"Are you sure?", "Confirm", JOptionPane.OK_CANCEL_OPTION,
							JOptionPane.QUESTION_MESSAGE);
					
					if (selectionForDel == 0) {
						//--------------PROGRESSSSSSSS---------!!!!!-
						for (ImageFotoPan tmpPan : fotosArr){
							this.objWorkDBImage.delImage(tmpPan.getID());
						}
					
						this.previewFotoPanel.deleteItems(true);
					}
					else {
						this.previewFotoPanel.deleteItems(false);
					}
				}
				else {
					JOptionPane.showMessageDialog(parent, "Choose nothing");
				}
			

		}
	}
	
	private void menuEvents(ActionEvent event){
		
		//-----------------MENU CONTROL-----------
		
		if (event.getSource() == this.menuItemExit){
			this.windowClosing(null);
		}
		else if (event.getSource() == this.menuItemHelp){
			JOptionPane.showMessageDialog(parent, "Create by ETR", "About",JOptionPane.INFORMATION_MESSAGE );
		}
		else if (event.getSource() == this.menuItemPropSet){
			System.out.println("Setting default image");
			File imageFile;
			this.fileChooser.setMultiSelectionEnabled(false);
			this.fileChooser.showOpenDialog(this.parent);
			imageFile = this.fileChooser.getSelectedFile();
			
			if (imageFile != null){
			
				try {
					BufferedImage image = ImageIO.read(imageFile);
					this.objWorkDBInfo.setImgIntoDfltTbl(image);
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
			/*else {
				System.out.println("Choos nothing");
				this.objWorkDBInfo.setImgIntoDfltTbl(null);
			}*/
			
			this.leftPanel.setItemList(this.objWorkDBInfo.getInfoForLPan());

		}
		else if (event.getSource() == this.menuItemPropReset){
			
			System.out.println("Choos Reset default Image");
			this.objWorkDBInfo.setImgIntoDfltTbl(null);
			this.leftPanel.setItemList(this.objWorkDBInfo.getInfoForLPan());
		}
		
		//----------UNLOAD IMAGES INTO DIRECTORY----------------
		else if (event.getSource() == this.menuItemUnload){
			if (this.unloadDialog == null){
				this.unloadDialog = new UnloadDialog(parent);
				//this.unloadDialog.filListItem();
				this.unloadDialog.setVisible(true);
			}
			else {
				this.unloadDialog.filListItem();
				this.unloadDialog.setVisible(true);
			}
		}
	}
	
	private void unloadImageEvents(ActionEvent event){
		//------------UNLOAD IMAGE----------------
		 if (event.getSource() == this.buttonUnload){
			System.out.println("UNLOAD IMAGES");
			
			File dirForUnload = unloadDialog.getTargetPath();
			if (dirForUnload != null){
				//this.unloadDialog.setVisible(false);
				this.unloadDialog.setAllInactive();
				Integer id = this.unloadDialog.getItemID();
				
				this.objWorkDBImage.unloadImages(id, dirForUnload, unloadDialog.getProgressBar());
			}
			else {
				JOptionPane.showMessageDialog(parent, "Choose directory!", "Directory?", JOptionPane.WARNING_MESSAGE);
			}
		}
		else if (event.getSource() == this.buttonCancelExport){
			System.out.println("Try interupt Unload");
			this.objWorkDBImage.interruptUnload();
			unloadDialog.getProgressBar().setValue(0);
		}
	}
	
	private void imageViewEvents(ActionEvent event){
		if (event.getSource() == this.nextViewButton){
			System.out.println("Next IMAGE");
			
			if (this.fotoIndex == arrayOfImage.length -1) this.fotoIndex = 0;
			else this.fotoIndex++;
			
			ImageFotoPan tmp = (ImageFotoPan) arrayOfImage[fotoIndex];
			this.fotoID = tmp.getID();
			
			this.imageViewFrame.setImage(this.objWorkDBImage.getImage(this.fotoID));
			
		}
		else if (event.getSource() == this.previousViewButton){
			System.out.println("Previous IMAGE");
			
			if (this.fotoIndex == 0) this.fotoIndex = arrayOfImage.length - 1;
			else this.fotoIndex--;
			
			ImageFotoPan tmp = (ImageFotoPan) arrayOfImage[fotoIndex];
			this.fotoID = tmp.getID();
			
			this.imageViewFrame.setImage(this.objWorkDBImage.getImage(this.fotoID));
		}
	}
	
	//------------------------------------------------------------------------
	
	public void setFieldsOnLPan(int ident) {

	}

	public ArrayList<WrapperLPan> getInfoForLPan() {
		return this.objWorkDBInfo.getInfoForLPan();
	}
	
	public ArrayList<WrapperLPan> getInfoForUnloader() {
		return this.objWorkDBInfo.getInfoForUnloader();
	}

	public void getInfoForRPan(Integer id) {
		if (id != null) {
			if (this.imageViewFrame.isVisible()) this.imageViewFrame.setVisible(false);
			
			this.lastID = id;
			WrapperRPanInfo wrapper = this.objWorkDBInfo.getInfoForRpanInfo(id);
			this.lastWrapperForRPan = wrapper;
			this.infPanel.setAllInfo(wrapper);
			
			this.getImagesForFotoPanel(id); // Retrieve images 
			
		}
		else {
			this.infPanel.clearAllFields();
			this.lastID = null;
		}

	}

	public void getImagesForFotoPanel(Integer id)
	{
		if (id != null) {
			System.out.println("Call objWorkDB.getImages");
			
			this.imageADD.setEnabled(false);
			this.imageDEL.setEnabled(false);
			
			this.objWorkDBImage.getImages(id, progressBar);
		}
		else {
			this.previewFotoPanel.clearItemList();
		}
	}
	
	//-----------------------------------------------------
	
	public void leftPanelIsEmpty(boolean empt){
		if (empt){
			this.buttonEd.setVisible(false);
			this.buttonDel.setVisible(false);
			this.buttonCancel.setVisible(false);
			this.buttonSv.setVisible(false);
			
			this.imageADD.setEnabled(false);
			this.imageDEL.setEnabled(false);
		}
		/*else{
			this.buttonEd.setVisible(true);
			this.buttonDel.setVisible(true);
			this.buttonCancel.setVisible(true);
			this.buttonSv.setVisible(true);
			
			this.imageADD.setEnabled(true);
			this.imageDEL.setEnabled(true);
		}*/
	}
	
	//==============END of SET IMAGE THREAD=================
	
	//------------Thread image DONE-------------------------
	
	public void finishSetImage(Integer id)
	{
		if (this.lastID == id){
			this.previewFotoPanel.setItemList(this.objWorkDBImage.getImages(id, progressBar));
		}
		this.leftPanel.setSelectedModeForPictPan(true);
		
		
	}
	
	public void finishGetImage(ArrayList<WrapperFotoPan> list)
	{
		System.out.println("GET images thread is FINISHED");
		this.previewFotoPanel.setItemList(list);
		this.progressBar.setValue(0);
		list = null;
		
		this.imageADD.setEnabled(true);
		this.imageDEL.setEnabled(true);
		
		this.buttonAdd.setEnabled(true);
		this.buttonEd.setEnabled(true);
		this.buttonDel.setEnabled(true);
		
	}
	
	public void finishUnloadImages(Boolean flg){
		System.out.println("-----FINISH UNLOADING----------");
		JProgressBar prBar = this.unloadDialog.getProgressBar();
		prBar.setValue(0);
		this.unloadDialog.setAllActive();
		//this.unloadDialog.setVisible(false);
	}
	
	//------------SHOW & HIDE View WINDOW------------------
	
	public void componentHidden(ComponentEvent arg0) {
		System.out.println("Hidden");
		this.imageADD.setEnabled(true);
		this.imageDEL.setEnabled(true);
		//-----delete current buffered image-----
		
		
	}
	
	public void componentShown(ComponentEvent arg0) {
		System.out.println("Shown");
		this.imageADD.setEnabled(false);
		this.imageDEL.setEnabled(false);
	}
	
	//-------------CLOSE MAIN WINDOW----------------------------
	
	public void windowClosing(WindowEvent arg0) {
		//System.out.println("Close MAIN PROGRAM WINDOW!!");
		this.objWorkDBImage.cancelAllThreads();
		System.exit(0);		
	}
	
	
	//===========================================================
	//------------------SETTERS----------------------------------

	public void setButtonAdd(JButton buttonAdd) {
		this.buttonAdd = buttonAdd;
	}

	public void setButtonEd(JButton buttonEd) {
		this.buttonEd = buttonEd;
	}

	public void setButtonSv(JButton buttonSv) {
		this.buttonSv = buttonSv;
	}

	public void setButtonDel(JButton buttonDel) {
		this.buttonDel = buttonDel;
		buttonSv.setVisible(false);
	}

	public void setButtonCancel(JButton buttonCancel) {
		this.buttonCancel = buttonCancel;
		buttonCancel.setVisible(false);
	}

	public void setInfPanel(InfoPanel infPan) {
		this.infPanel = infPan;
	}

	public void setLeftPanel(LPanel leftPanel) {
		this.leftPanel = leftPanel;
	}

	public void setParetn(JFrame paretn) {
		this.parent = paretn;
	}
	
	public void setPreviewImagePanel(PreviewImagePanel fotoPanel) {
		this.previewFotoPanel = fotoPanel;
	}

	public void setFotoID(int fotoID) {
		this.fotoID = fotoID;
	}

	public void setProgressBar(JProgressBar progressBar) {
		this.progressBar = progressBar;
	}
	
	public void setImageADD(JButton imageADD) {
		this.imageADD = imageADD;
	}

	public void setImageDEL(JButton imageDEL) {
		this.imageDEL = imageDEL;
	}

	public void setTabledPan(JTabbedPane tabledPan) {
		this.tabledPan = tabledPan;
	}
	
	public void setImageViewFrame(ImageViewFrame imageViewFrame) {
		this.imageViewFrame = imageViewFrame;
	}

	public void setNextViewButton(JButton nextViewButton) {
		this.nextViewButton = nextViewButton;
	}

	public void setPreviousViewButton(JButton previousViewButton) {
		this.previousViewButton = previousViewButton;
	}
	
	public void setMenuItemPropSet(JMenuItem menuItemProp) {
		this.menuItemPropSet = menuItemProp;
	}

	public void setMenuItemPropReset(JMenuItem menuItemPropReset) {
		this.menuItemPropReset = menuItemPropReset;
	}

	public void setMenuItemUnload(JMenuItem menuItemUnload) {
		this.menuItemUnload = menuItemUnload;
	}

	public void setMenuItemHelp(JMenuItem menuItemHelp) {
		this.menuItemHelp = menuItemHelp;
	}

	public void setMenuItemExit(JMenuItem menuItemExit) {
		this.menuItemExit = menuItemExit;
	}
	
	public void setButtonUnload(JButton buttonUnload) {
		this.buttonUnload = buttonUnload;
	}
	
	public void setButtonCancelExport(JButton buttonCancelExport) {
		this.buttonCancelExport = buttonCancelExport;
	}

	public void setTextFieldName(JTextField textFieldName) {
		this.textFieldName = textFieldName;
	}

	public void setTextFieldSurname(JTextField textFieldSurname) {
		this.textFieldSurname = textFieldSurname;
	}
	
	public void setArrayOfImage(Object[] arrayOfImage) {
		this.arrayOfImage = arrayOfImage;
	}
	
	public void setFotoIndex(int fotoIndex) {
		this.fotoIndex = fotoIndex;
	}
	//----------Inner classes for File Chooser----------
	// ---------------------------------------------------------------------
	// --------------Inner CLASSES------------------------------------------

	private class FilterForChooser extends FileFilter {

		public boolean accept(File f) {

			boolean cond1 = f.getName().toLowerCase().endsWith(".jpg");
			boolean cond2 = f.getName().toLowerCase().endsWith(".jpeg");
			boolean cond3 = f.getName().toLowerCase().endsWith(".gif");
			boolean cond4 = f.isDirectory();

			if (cond1 || cond2 || cond3 || cond4)
				return true;
			else
				return false;
		}

		public String getDescription() {

			return "Image Files ( JPG )";
		}

	}

	private class ImagePreviewer extends JLabel {
		public ImagePreviewer(JFileChooser chooser) {
			this.setPreferredSize(new Dimension(150, 150));
			this.setBorder(BorderFactory.createEtchedBorder());

			chooser.addPropertyChangeListener(new PropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent evt) {
					if (evt.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
						File f = (File) evt.getNewValue();
						if (f == null) {
							setIcon(null);
							return;
						}

						ImageIcon icon = new ImageIcon(f.getPath());
						if (icon.getIconWidth() > getWidth()) {
							icon = new ImageIcon(icon.getImage()
									.getScaledInstance(getWidth(), -1,
											Image.SCALE_DEFAULT));
						}

						setIcon(icon);
					}
				}
			});
		}
	}
	
	//-----interfaces remains---------------------------------------------
	
	public void componentMoved(ComponentEvent arg0) {}

	public void componentResized(ComponentEvent arg0) {}

	@Override
	public void changedUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}	
}
